//
//  PositiveNegativeBarChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "PositiveNegativeBarChartViewController.h"

@interface PositiveNegativeBarChartViewController () <ChartViewDelegate>

@end

@implementation PositiveNegativeBarChartViewController
BarChartView *samplePositiveNegativeBarChartView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}
-(void)initChartView{
    
    samplePositiveNegativeBarChartView=[[HorizontalBarChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    samplePositiveNegativeBarChartView.backgroundColor = [UIColor whiteColor];
    samplePositiveNegativeBarChartView.delegate = self;
    [self.view addSubview:samplePositiveNegativeBarChartView];
    [self initHorizontalBarChartStyleAndData];
}

-(void)initHorizontalBarChartStyleAndData{
    [self setupBarChartView:samplePositiveNegativeBarChartView];
    [self setBarChartParam:samplePositiveNegativeBarChartView];
    [self setLegendStyle:samplePositiveNegativeBarChartView];
    [self initChartData];
}

- (void)setupBarChartView:(BarChartView *)chartView{
    chartView.extraTopOffset = -30.f;
    chartView.extraBottomOffset = 10.f;
    chartView.extraLeftOffset = 70.f;
    chartView.extraRightOffset = 70.f;
    
    chartView.drawBarShadowEnabled = NO;
    chartView.drawValueAboveBarEnabled = YES;
    
    chartView.descriptionText = @"";
    
    // scaling can now only be done on x- and y-axis separately
    chartView.pinchZoomEnabled = NO;
    
    chartView.drawGridBackgroundEnabled = NO;
}

-(void)setBarChartParam:(BarChartView *)chartView{
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelFont = [UIFont systemFontOfSize:13.f];
    xAxis.drawGridLinesEnabled = NO;
    xAxis.drawAxisLineEnabled = NO;
    xAxis.spaceBetweenLabels = 2.0;
    xAxis.labelTextColor = [UIColor lightGrayColor];
    
    ChartYAxis *leftAxis = chartView.leftAxis;
    leftAxis.drawLabelsEnabled = NO;
    leftAxis.spaceTop = 0.25f;
    leftAxis.spaceBottom = 0.25f;
    leftAxis.drawAxisLineEnabled = NO;
    leftAxis.drawGridLinesEnabled = NO;
    leftAxis.drawZeroLineEnabled = YES;
    leftAxis.zeroLineColor = UIColor.grayColor;
    leftAxis.zeroLineWidth = 0.7f;
    
    chartView.rightAxis.enabled = NO;
    
}

//设置图例样式
-(void)setLegendStyle:(BarChartView *)chartView{
    chartView.legend.enabled = NO;
}

- (void)initChartData
{
    [self setChartData:samplePositiveNegativeBarChartView];
}



- (void)setChartData:(BarChartView *)chartView
{
    // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
    NSArray<NSDictionary *> *dataList = @[
                                          @{@"xIndex": @(0),
                                            @"yValue": @(-224.1f),
                                            @"xValue": @"12-19"},
                                          @{@"xIndex": @(1),
                                            @"yValue": @(238.5f),
                                            @"xValue": @"12-30"},
                                          @{@"xIndex": @(2),
                                            @"yValue": @(1280.1f),
                                            @"xValue": @"12-31"},
                                          @{@"xIndex": @(3),
                                            @"yValue": @(-442.3f),
                                            @"xValue": @"01-01"},
                                          @{@"xIndex": @(4),
                                            @"yValue": @(-2280.1f),
                                            @"xValue": @"01-02"},
                                          ];
    
    NSMutableArray<BarChartDataEntry *> *values = [[NSMutableArray alloc] init];
    NSMutableArray<NSString *> *dates = [[NSMutableArray alloc] init];
    NSMutableArray<UIColor *> *colors = [[NSMutableArray alloc] init];
    
    UIColor *green = [UIColor colorWithRed:110/255.f green:190/255.f blue:102/255.f alpha:1.f];
    UIColor *red = [UIColor colorWithRed:211/255.f green:74/255.f blue:88/255.f alpha:1.f];
    
    for (int i = 0; i < dataList.count; i++)
    {
        NSDictionary *d = dataList[i];
        BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithValue:[d[@"yValue"] doubleValue] xIndex:[d[@"xIndex"] integerValue]];
        [values addObject:entry];
        
        [dates addObject:d[@"xValue"]];
        
        // specific colors
        if ([d[@"yValue"] doubleValue] >= 0.f)
        {
            [colors addObject:red];
        }
        else
        {
            [colors addObject:green];
        }
    }
    
    BarChartDataSet *set = set = [[BarChartDataSet alloc] initWithYVals:values label:@"Values"];
    set.barSpace = 0.4f;
    set.colors = colors;
    set.valueColors = colors;
    
    BarChartData *data = [[BarChartData alloc] initWithXVals:dates dataSet:set];
    [data setValueFont:[UIFont systemFontOfSize:13.f]];
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.maximumFractionDigits = 1;
    [data setValueFormatter:formatter];
    
    chartView.data = data;
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected, stack-index %ld", (long)highlight.stackIndex);
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end
